import 'package:mall/constant/app_parameters.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/app_urls.dart';
import 'package:mall/modules/model/cart_entity.dart';
import 'package:mall/modules/model/fill_in_order_entity.dart';
import 'package:mall/utils/broadcast/broadcast_util.dart';
import 'package:mall/utils/net/http_util.dart';

//购物车事件
abstract class CartEvent {
  // 添加购物车
  void addCart(int goodsId, int productId, int number);
  // 购物车结算
  void cartCheckOut(int cartId);
  // 购物车选中
  void cartCheck(List<int> productId, bool isCheck);
  // 删除购物车
  void deleteCart(List<int> productIds);
  // 更新购物车
  void updateCart(
    int productId,
    int goodsId,
    int number,
    int cartId,
  );
}

class CartService {
// 添加购物车
  static Future<void> addCart(int goodsId, int productId, int number) async {
    var parameters = {
      AppParameters.GOODS_ID: goodsId,
      AppParameters.PRODUCT_ID: productId,
      AppParameters.NUMBER: number
    };

    await HttpUtil.instance.post(AppUrls.ADD_CART, parameters: parameters);

    BroadcastUtil.notify<CartEvent>((p0) => p0.addCart(goodsId, productId, number));
  }

// 购物车结算
  static Future<FillInOrderEntity> cartCheckOut(int cartId) async {
    var parameters = {
      AppParameters.CART_ID: cartId,
      AppParameters.ADDRESS_ID: 0,
      AppParameters.COUPON_ID: 0,
      AppParameters.GROUPON_RULES_ID: 0,
    };

    var response = await HttpUtil.instance.get(AppUrls.CART_BUY, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }
    final FillInOrderEntity res = FillInOrderEntity.fromJson(response[AppStrings.DATA]);
    BroadcastUtil.notify<CartEvent>((p0) => p0.cartCheckOut(cartId));
    return res;
  }

// 查询购物车
  static Future<CartEntity> queryCart() async {
    var response = await HttpUtil.instance.get(AppUrls.CART_LIST);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    return CartEntity.fromJson(response[AppStrings.DATA]);
  }

// 购物车选中
  static Future<CartEntity> cartCheck(List<int> productId, bool isCheck) async {
    var parameters = {
      AppParameters.PRODUCT_IDS: productId,
      AppParameters.IS_CHECKED: isCheck ? 1 : 0,
    };

    var response = await HttpUtil.instance.post(AppUrls.CART_CHECK, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    final CartEntity res = CartEntity.fromJson(response[AppStrings.DATA]);
    BroadcastUtil.notify<CartEvent>((p0) => p0.cartCheck(productId, isCheck));
    return res;
  }

// 删除购物车
  static Future<CartEntity> deleteCart(List<int> productIds) async {
    var parameters = {AppParameters.PRODUCT_IDS: productIds};

    var response = await HttpUtil.instance.post(AppUrls.CART_DELETE, parameters: parameters);

    if (response[AppStrings.DATA] == null) {
      throw Exception(AppStrings.SERVER_EXCEPTION);
    }

    final CartEntity res = CartEntity.fromJson(response[AppStrings.DATA]);
    BroadcastUtil.notify<CartEvent>((p0) => p0.deleteCart(productIds));
    return res;
  }

// 更新购物车
  static Future<void> updateCart({
   required int productId,
   required int goodsId,
   required int number,
   required int cartId,
  }) async {
    var parameters = {
      AppParameters.PRODUCT_ID: productId,
      AppParameters.GOODS_ID: goodsId,
      AppParameters.NUMBER: number,
      AppParameters.ID: cartId,
    };

    await HttpUtil.instance.post(AppUrls.CART_UPDATE, parameters: parameters);

    BroadcastUtil.notify<CartEvent>((p0) => p0.updateCart(productId, goodsId, number, cartId));
  }
}
